/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.library.view.helper;


import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.utils.PixelMapUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;


/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class CardViewHelperImplBase implements CardViewHelper {

    protected Context mContext;

    public CardViewHelperImplBase(Context context) {
        mContext = context;
    }

    @Override
    public void setBackground(Component view, Element drawable) {

        if (view != null) {
            if(view instanceof Image){
                ((Image)view).setImageElement(drawable);
            }else{
                view.setBackground(drawable);
            }

        }


    }

    @Override
    public void setButtonBackground(Image imageButton, int buttonDrawableResource) {
        try {
            setBackground(imageButton, PixelMapUtils.prepareElementById(mContext, buttonDrawableResource));
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
    }

    @Override
    public void setCardSelector(Component viewClickable, Element defaultDrawable) {
        setBackground(viewClickable, defaultDrawable);
    }

    @Override
    public void setElevation(Component view, float elevation) {
        //todo no such method
//        ViewCompat.setElevation(view, elevation);
    }

    @Override
    public Element getResourceFromAttrs(Context themedContext, AttrSet attr) {


        //homs get ItemBackground
        Element drawableFromTheme = attr.getAttr("selectableItemBackground").get().getElement();

        return drawableFromTheme;
    }

}
